package com.ctrip.ec.api.admin.controller.role;
import com.ctrip.ec.api.admin.common.ValidateException;
import com.ctrip.ec.api.admin.model.query.QueryModel;
import com.ctrip.ec.biz.dao.module.ModuleRoleDao;
import com.ctrip.ec.biz.entity.role.SysRole;
import com.ctrip.ec.dal.PagerSetting;
import com.ctrip.ec.api.admin.common.RequestContext;
import com.ctrip.ec.biz.dao.role.RoleDao;
import com.ctrip.ec.biz.entity.role.RoleEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("role")
public class RoleController{
    @Autowired
    private RoleDao role;

    @Autowired
    private ModuleRoleDao moduleRole;

    @GetMapping("list")
    public List<RoleEntity> list() throws SQLException {
        List<RoleEntity> list = role.list();
        return list;
    }

    @PostMapping("list")
    public List<RoleEntity> list(@RequestBody QueryModel model) throws  SQLException{
        PagerSetting setting = model.build();
        List<RoleEntity> list = role.list(setting);
        if(model.getPageIndex() == null || model.getPageIndex() == 1) {
            for (SysRole sysRole : SysRole.values()) {
                RoleEntity roleEntity = new RoleEntity();
                roleEntity.setId(sysRole.getId());
                roleEntity.setCode(sysRole.name());
                roleEntity.setName(sysRole.getName());
                roleEntity.setRemark("系统角色,不能删除!");
                roleEntity.setTypeId(0);
                roleEntity.setSortNum(0);
                list.add(0,roleEntity);

            }
        }
        RequestContext.setResponseHeaderCount(setting.getRecordCount());
        return list;
    }

    @GetMapping("detail/{id}")
    public  RoleEntity detail(@PathVariable Long id) throws SQLException {
        return role.entity(id);
    }

    @PostMapping("save")
    public  Long save(@RequestBody RoleEntity entity) throws SQLException {

        Timestamp now = new Timestamp(System.currentTimeMillis());
        if(entity.add()) {
            entity.setCreateTime(now);
        }
        entity.setUpdateTime(now);
        role.save(entity);
        return entity.getId();
    }

    @PostMapping("delete")
    public  void delete(@RequestBody List<Long> ids) throws SQLException {

        int size =  ids.stream().filter(t->t<0L).collect(Collectors.toList()).size();
        if(size>0){
            throw new ValidateException("系统角色不能删除");
        }
        role.delete(ids);
    }
    @PostMapping("erase")
    public void erase(@RequestBody List<Long> ids) throws  SQLException{
        role.erase(ids);
    }

    @PostMapping("unerase")
    public  void unerase(@RequestBody List<Long> ids) throws  SQLException{
        role.unerase(ids);
    }

    @PostMapping("saveModule")
    public  void saveModule(@RequestBody List<Long> moduleIds,@RequestParam Long roleId) throws SQLException {
        moduleRole.saveByRoleId(moduleIds,roleId);
    }

    @GetMapping("listModule")
    public  List<Long> listModule(@RequestParam Long roleId) throws SQLException {
        return moduleRole.listByRoleId(roleId);
    }
}
